package com.sh.stream流;

import java.util.*;

/**
 * @Auther: sh
 * @Version 1.0
 * @Description:  不可变集合
 */
public class ImmutableDemo {
    public static void main(String[] args) {
        // 不可变集合
        List<String> strList = List.of("a", "b");
//        strList.add("A"); // 异常
//        System.out.println(strList.get(0));
        Set<String> strSet = Set.of("a", "b", "c");
        Iterator<String> iterator = strSet.iterator();
        while (iterator.hasNext()) {
            String next = iterator.next();
            System.out.println(next);
        }
        // 最多只能加20个键值对，因为可变参数必须写在最后，这里不能实现2个可变参数
        Map<String, String> strMap = Map.of("a", "A", "b", "B");
        for (Map.Entry<String, String> stringStringEntry : strMap.entrySet()) {
            String value = stringStringEntry.getValue();
            String key = stringStringEntry.getKey();
            System.out.println(key + "\t" + value);
        }
        Set<String> keyS = strMap.keySet();
        for (String key : keyS) {

            String s = strMap.get(key);
            System.out.println(key + "\t" + s);
        }

        // 生成不可变的map集合，且不限制数量
        HashMap<Object, Object> objectObjectHashMap = new HashMap<>();
        objectObjectHashMap.put("a", "a");
        Map.ofEntries(objectObjectHashMap.entrySet().toArray(new Map.Entry[1]));

        Map<Object, Object> objectObjectMap = Map.copyOf(objectObjectHashMap);
        objectObjectMap.put("b", "b");


    }
}

